// 留言板替换验证码
$('.user-message-send-captcha').click(function () {
    $(".user-message-send-captcha").attr("src", '/getCode?' + Math.random());//jquery方式
})

//保存留言
function saveMessageBoard() {
    let saveMessageBoardUrl = "/v1/rest/messageBoard/saveMessageBoard";
    var message = $(".user-input-message").val();
    var code = $(".user-message-code").val();
    if(message == "" || !message){
        alert("请输入留言内容");
        return false;
    }
    if(code == "" || !code){
        alert("请输入验证码");
        return false;
    }
    let data = {
        content:message,
        code:code
    };
    $.ajax({
        type: "POST",
        url: saveMessageBoardUrl,
        contentType: "application/json", //必须有
        dataType: "json", //表示返回值类型，不必须
        data: JSON.stringify(data),
        success: function (jsonResult) {
            if (jsonResult.code == 200) {
                $(".user-input-message").val("");
                $(".user-input-message").val("");
                alert("留言成功").then(()=>{
                    window.location.reload();
                });
            } else {
                alert(jsonResult.msg);
            }
        }
    });
}



//留言查看更多
function getMessageBoardList() {
    if ($(".message-more div").text() == "没有更多了") {
        return false;
    }
    let page = $('.message-page').val();
    page++;
    let messageBoardUrl = "/v1/rest/messageBoard/list/" + page + "/5";
    let data = {};
    $.ajax({
        type: "POST",
        url: messageBoardUrl,
        contentType: "application/json", //必须有
        dataType: "json", //表示返回值类型，不必须
        data: JSON.stringify(data),
        success: function (jsonResult) {
            if (jsonResult.code == 200) {
                $.each(jsonResult.data.records, function (index, value) {
                    var clone = $('.message-item-template').clone();
                    clone.find('.head-portrait').attr("src", value.avatar);
                    clone.find('.time').html(value.createTime);
                    clone.find('.content').html(value.content);
                    if (!value.replyContent) {
                        clone.find('.message-item-reply-main').remove();
                    } else {
                        clone.find('.message-item-reply-main .time').html(value.replyTime);
                        clone.find('.message-item-reply-main .content').html(value.replyContent);
                    }
                    clone.removeClass('message-item-template').show().addClass("message-item");
                    $('.message-list').append(clone);
                });
                $('.message-page').val(jsonResult.data.current);
                if (jsonResult.data.pages == page || jsonResult.data.pages <= jsonResult.data.current) {
                    $(".message-more div").text("没有更多了");
                }


            } else {
                alert(jsonResult.msg);
            }
        }
    });
}




/*
 * 剩余字数统计
 * 注意 最大字数只需要在放数字的节点哪里直接写好即可 如：<var class="word">200</var>
 * 例如：statInputNum($('.user-input-message'),$('.user-input-word'));
 */
function statInputNum(textArea, numItem) {
    var max = numItem.text(),
        curLength;
    textArea[0].setAttribute("maxlength", max);
    curLength = textArea.val().length;
    numItem.text(max - curLength);
    textArea.on('input propertychange', function() {
        var _value = $(this).val().replace(/\n/gi, "");
        numItem.text(max - _value.length);
        // 留言
        if(textArea.val().length > 0){
            $('.user-message-send-btn').addClass("on");
        }else{
            $('.user-message-send-btn').removeClass("on");
        }
    });
}
// 留言字数计算
if ($('.user-input-message').length != 0) {
    statInputNum($('.user-input-message'), $('.user-input-word'));
}


